module.exports = {
  up: async (queryInterface, Sequelize) => {
    // 3. 表格按钮配置表
    await queryInterface.createTable('table_buttons', {
      id: {
        type: Sequelize.UUID,
        defaultValue: Sequelize.UUIDV4,
        primaryKey: true,
      },
      table_id: {
        type: Sequelize.UUID,
        allowNull: false,
        references: {
          model: 'table_configs',
          key: 'id',
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
      },
      code: {
        type: Sequelize.STRING(100),
        allowNull: false,
        comment: '按钮唯一标识',
      },
      label: {
        type: Sequelize.STRING(50),
        allowNull: false,
        comment: '按钮文字',
      },
      type: {
        type: Sequelize.ENUM('primary', 'success', 'warning', 'danger', 'info', 'text'),
        allowNull: false,
        defaultValue: 'primary',
        comment: '按钮类型',
      },
      icon: {
        type: Sequelize.STRING(100),
        allowNull: true,
        comment: '图标',
      },
      position: {
        type: Sequelize.ENUM('toolbar', 'row'),
        allowNull: false,
        defaultValue: 'row',
        comment: '按钮位置: toolbar工具栏, row行内',
      },
      action_type: {
        type: Sequelize.STRING(50),
        allowNull: false,
        comment: '操作类型: route, api, dialog, drawer, confirm等',
      },
      action_config: {
        type: Sequelize.JSON,
        allowNull: true,
        comment: '操作配置',
      },
      permission_code: {
        type: Sequelize.STRING(100),
        allowNull: true,
        comment: '权限码，用于控制显示',
      },
      confirm_message: {
        type: Sequelize.STRING(255),
        allowNull: true,
        comment: '确认提示',
      },
      is_visible: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: true,
        comment: '是否显示',
      },
      is_disabled: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: false,
        comment: '是否禁用',
      },
      visible_condition: {
        type: Sequelize.JSON,
        allowNull: true,
        comment: '显示条件配置',
      },
      sort_order: {
        type: Sequelize.INTEGER,
        allowNull: false,
        defaultValue: 0,
      },
      created_at: {
        type: Sequelize.DATE,
        allowNull: false,
      },
      updated_at: {
        type: Sequelize.DATE,
        allowNull: false,
      },
    });

    await queryInterface.addIndex('table_buttons', ['table_id']);
    await queryInterface.addIndex('table_buttons', ['code']);
    await queryInterface.addIndex('table_buttons', ['position']);
    await queryInterface.addIndex('table_buttons', ['is_visible']);
  },

  down: async (queryInterface) => {
    await queryInterface.dropTable('table_buttons');
  },
};


